package com.xiaoma.bingox.web;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.dexcoder.commons.pager.Pager;
import com.xiaoma.bingox.dto.RoleResourceDto;
import com.xiaoma.bingox.entity.Role;
import com.xiaoma.bingox.entity.RoleResource;
import com.xiaoma.bingox.entity.User;
import com.xiaoma.bingox.service.RoleService;
import com.xiaoma.bingox.service.UserService;

@Controller
@RequestMapping("/app/role")
public class RoleController {

    @Autowired
    private RoleService roleService;
    
    @Autowired
    private UserService userService;
    
    @RequestMapping(method = RequestMethod.GET)
    public String index(){
        return "/role/index";
    }
    
    @RequestMapping(value="/datas",method=RequestMethod.GET)
    @ResponseBody
    public Map<String,Object> loadDatas(Integer page, Integer rows){
        page = page == null ? 1 : page;
        rows = rows == null ? 50 : rows;
        Map<String,Object> result = new HashMap<String, Object>();
        Pager pager = roleService.findAll(page, rows);
        result.put("rows", pager.getList());
        result.put("total", pager.getItemsTotal());
        return result;
    }
    
    @RequestMapping(value="/save", method = RequestMethod.POST)
    @ResponseBody
    public Map<String,Object> save(Role role){
        Map<String,Object> result = new HashMap<String, Object>();
        try {
            roleService.save(role);
            result.put("status", 1);
        } catch(Exception e) {
            result.put("status", 0);
        }
        return result;
    } 
    
    
    @RequestMapping(value="/update", method = RequestMethod.POST)
    @ResponseBody
    public Map<String,Object> update(Role role){
        Map<String,Object> result = new HashMap<String, Object>();
        try {
            roleService.update(role);
            result.put("status", 1);
        } catch(Exception e) {
            result.put("status", 0);
            result.put("message", e.getMessage());
        }
        return result;
    } 
    
    @RequestMapping(value="/delete/{id}", method = RequestMethod.POST)
    @ResponseBody
    public Map<String,Object> delete(@PathVariable Integer id){
        Map<String,Object> result = new HashMap<String, Object>();
        try {
            User entity = new User();
            entity.setRoleId(id);
            List<User> users = userService.find(entity);
            if (users.size() > 0) {
                result.put("status", 0);
                result.put("message", "该角色已经被引用，无法删除");
            } else {
                roleService.delete(id);
                RoleResource rr = new RoleResource();
                rr.setRoleId(id);
                roleService.delete(rr);
                result.put("status", 1);
                
            }
        } catch(Exception e) {
            result.put("status", 0);
            result.put("message", e.getMessage());
        }
        return result;
    }
    
    @RequestMapping(value="/allocate", method = RequestMethod.POST)
    @ResponseBody
    public Map<String,Object> allocate(@RequestBody RoleResourceDto roleResourceDto){
        Map<String,Object> result = new HashMap<String, Object>();
        try {
            RoleResource entity = new RoleResource();
            entity.setRoleId(roleResourceDto.getRoleId());
            roleService.delete(entity);
            for (int resourceId : roleResourceDto.getResourceIds()) {
                RoleResource roleResource = new RoleResource();
                roleResource.setRoleId(roleResourceDto.getRoleId());
                roleResource.setResourceId(resourceId);
                roleService.save(roleResource);
            }
            result.put("status", 1);
        } catch(Exception e) {
            result.put("status", 0);
            result.put("message", e.getMessage());
        }
        return result;
    }
    
    //role 下拉框数据
    @RequestMapping(value="/selectDatas",method=RequestMethod.GET)
    @ResponseBody
    public List<Role> selectDatas(){
        return roleService.findSelectDatas();
    }
}
